import json

import numpy as np
from django.test import TestCase

# Create your tests here.
from BusinessSubSystem.algorithmFunction import fund_selection_rule, AHP, bond_fund_selection_rules, get_weight_by_ahp
from BusinessSubSystem.constUtils import VERY_SIGNIFICANTLY_HIGHER_THAN_ANOTHER
from BusinessSubSystem.models import fundField, fundToFundField, fund
from BusinessSubSystem.notSaveInDataBaseModel import FundListSingleton
from CoreSubSystem.models import user
from UserSubSystem.models import card


class BusinessSubSystemTests(TestCase):

    def setUp(self) -> None:
        fundField.objects.create(
            id=1,
            fund_field_name="非银金融"
        )

        fund.objects.create(
            id=1,
            fund_code="003624",
            fund_name="创金合信资源主题精选股票A",
            fund_type="stock_fund",
            fund_asset_val="950924440.13",
            stock_percent="88.05",
            cash_percent="6.56",
            bond_percent="5.16",
            other_percent="0.23",
            investment_cost_performance_1y="87%",
            risk_control_1y="29%",
            volatility_rank_1y="0.342",
            sharpe_rank_1y="0.1",
            max_draw_down_1y="0.3616",
            investment_cost_performance_3y="89%",
            risk_control_3y="32%",
            volatility_rank_3y="0.3113",
            sharpe_rank_3y="1.15",
            max_draw_down_3y="0.3616",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.7131480579",
            nav_grl1y="4.3006448744",
            nav_grl2y="116.5266365063",
            nav_grl3y="173.9079654286",
            srank_l1m="5/1369",
            srank_l3m="4/1322",
            srank_l6m="6/1218",
            srank_l1y="7/1024",
            srank_l3y="24/609",
            srank_base="5/1381"
        )

        fund.objects.create(
            id=2,
            fund_code="001070",
            fund_name="建信信息产业股票",
            fund_type="stock_fund",
            fund_asset_val="1019048105.05",
            stock_percent="80.83",
            cash_percent="18.11",
            bond_percent="0.98",
            other_percent="0.08",
            investment_cost_performance_1y="85%",
            risk_control_1y="55%",
            volatility_rank_1y="0.2572",
            sharpe_rank_1y="0.0",
            max_draw_down_1y="0.3358",
            investment_cost_performance_3y="78%",
            risk_control_3y="55%",
            volatility_rank_3y="0.2576",
            sharpe_rank_3y="0.99",
            max_draw_down_3y="0.3358",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.1773543422",
            nav_grl1y="13.4965266292",
            nav_grl2y="-23.4932709187",
            nav_grl3y="-0.3049942814",
            srank_l1m="1/1369",
            srank_l3m="1/1322",
            srank_l6m="7/1218",
            srank_l1y="4/1024",
            srank_l3y="20/609",
            srank_base="4/1381"
        )

        fund.objects.create(
            id=99,
            fund_code="008865",
            fund_name="鑫元中短债C",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="14/3784",
            srank_l3m="8/3696",
            srank_l6m="4/3477",
            srank_l1y="8/3066",
            srank_base="22/3781"
        )

        fundToFundField.objects.create(
            id=1,
            fund_id_id=1,
            fund_field_id_id=1,
            fund_percent="57.89"
        )

        aUser = user.objects.create(
            id=1,
            user_name="Yiyang",
            user_real_name="陈王艺阳",
            user_password="123456789",
            user_wallet_balance=7777.77,
            user_phone_number="13569998888",
            user_annual_income=5000.00,
            user_expected_time=5,
            user_expected_return_rate=15.55,
            user_risk_ratio=36.78
        )

        card.objects.create(
            id=1,
            card_bank="中国银行",
            card_type="信用卡",
            # 信用卡都为16位
            card_number="1111111111111111",
            card_password="123456",
            card_balance=1234.56,
            card_single_limit=500.00,
            card_daily_limit=1200.00,
            # 外键
            user_id_id=aUser.id
        )

    # fundField Model模型类测试
    def test_fundField_model(self):
        print("fundField模型类测试开始")
        result = fundField.objects.get(id=1)
        self.assertEqual(result.fund_field_name, "非银金融")
        fundField.objects.filter(id=1).update(fund_field_name="新冠研究")
        result = fundField.objects.get(id=1)
        self.assertEqual(result.fund_field_name, "新冠研究")
        fundField.objects.filter(id=1).delete()
        # 删除后不存在
        self.assertFalse(fundField.objects.filter(id=1))
        print("fundField模型类测试完成")

    # fund Model模型类测试
    def test_fund_model(self):
        print("fund模型类测试开始")
        result = fund.objects.get(id=1)
        self.assertEqual(result.fund_code, "003624")
        self.assertEqual(result.fund_name, "创金合信资源主题精选股票A")
        self.assertEqual(result.fund_type, "stock_fund")
        self.assertEqual(result.fund_asset_val, "950924440.13")
        self.assertEqual(result.stock_percent, "88.05")
        self.assertEqual(result.cash_percent, "6.56")
        self.assertEqual(result.bond_percent, "5.16")
        self.assertEqual(result.other_percent, "0.23")
        self.assertEqual(result.investment_cost_performance_1y, "87%")
        self.assertEqual(result.risk_control_1y, "29%")
        self.assertEqual(result.volatility_rank_1y, "0.342")
        self.assertEqual(result.sharpe_rank_1y, "0.1")
        self.assertEqual(result.max_draw_down_1y, "0.3616")
        self.assertEqual(result.investment_cost_performance_3y, "89%")
        self.assertEqual(result.risk_control_3y, "32%")
        self.assertEqual(result.volatility_rank_3y, "0.3113")
        self.assertEqual(result.sharpe_rank_3y, "1.15")
        self.assertEqual(result.max_draw_down_3y, "0.3616")
        self.assertEqual(result.nav_grl1m, "-5.8908507223")
        self.assertEqual(result.nav_grl3m, "-10.7440532826")
        self.assertEqual(result.nav_grl6m, "-4.7131480579")
        self.assertEqual(result.nav_grl1y, "4.3006448744")
        self.assertEqual(result.nav_grl2y, "116.5266365063")
        self.assertEqual(result.nav_grl3y, "173.9079654286")
        self.assertEqual(result.srank_l1m, "5/1369")
        self.assertEqual(result.srank_l3m, "4/1322")
        self.assertEqual(result.srank_l6m, "6/1218")
        self.assertEqual(result.srank_l1y, "7/1024")
        self.assertEqual(result.srank_l3y, "24/609")
        self.assertEqual(result.srank_base, "5/1381")
        print("fund模型类测试完成")

    # fundToFundField Model模型类测试
    def test_fundToFundField(self):
        print("fundToFundField模型类测试开始")
        result = fundToFundField.objects.get(id=1)
        self.assertEqual(result.fund_id_id, 1)
        self.assertEqual(result.fund_field_id_id, 1)
        self.assertEqual(result.fund_percent, "57.89")
        print("fundToFundField模型类测试完成")

    def test_get_all_fund_field_controller(self):
        fundField.objects.create(
            id=2,
            fund_field_name="银行"
        )
        fundField.objects.create(
            id=3,
            fund_field_name="家用电器"
        )
        fundField.objects.create(
            id=4,
            fund_field_name="其他"
        )

        result = self.client.get('/business/getAllFundFields')

        allFund = json.loads(json.loads(result.content)['data'])
        self.assertEqual(result.status_code, 200)
        self.assertEqual(allFund.__len__(), 3)
        self.assertEqual(allFund[0]['pk'], 1)
        self.assertEqual(allFund[0]['fields']['fund_field_name'], '非银金融')
        print(allFund.__len__())

    # receive_form方法测试
    def test_receive_form_controller(self):
        print("receive_form方法测试开始")
        # # 用户测评表单信息
        # data = {'user_form': ['{"user_id":"1","user_annual_income":"30","user_expected_time":"2","user_expected_return":"0.10","fund_field":[2,6,3,9]}']}
        # result = self.client.post("/business/receiveForm", data=data)
        # # 返回正常
        # self.assertEqual(result.status_code, 200)
        # self.assertEqual(json.loads(result.content)['data'], "success")
        # self.assertTrue(json.loads(result.content)['flag'])
        print("receive_form方法测试完成")

    def test_get_expected_rate_controller(self):
        print("get_expected_rate方法测试开始")
        result = self.client.get("/business/getExpectedRate?user_id=49")
        allRates = json.loads(result.content)['data']
        self.assertEqual(result.status_code, 200)
        self.assertEqual(allRates[0]['max_expected_return'], 0.0045000000000000005)
        self.assertEqual(allRates[0]['min_expected_return'], 0.0015)
        self.assertEqual(allRates[0]['user_expected_time'], '1')
        print("get_expected_rate方法测试完成")

    # adjust_risk_assets_rate方法测试
    def test_adjust_risk_assets_rate_controller(self):
        print("adjust_risk_assets_rate方法测试开始")
        data = {'user_adjust_risk_assets_rate_form': ['{"user_id":"49","user_risk_ratio":0.45}']}
        result = self.client.post("/business/adjustRiskAssetsRate", data=data)
        self.assertEqual(result.status_code, 200)
        self.assertEqual(json.loads(result.content)['data'], "success")
        self.assertTrue(json.loads(result.content)['flag'])
        print("adjust_risk_assets_rate方法测试完成")

    def test_get_scheme_one_controller(self):
        print("get_scheme_one方法测试开始")

        this = FundListSingleton.get_instance()
        fund_one = fund.objects.create(
            id=3,
            fund_code="003624",
            fund_name="创金合信资源主题精选股票A",
            fund_type="stock_fund",
            fund_asset_val="950924440.13",
            stock_percent="88.05",
            cash_percent="6.56",
            bond_percent="5.16",
            other_percent="0.23",
            investment_cost_performance_1y="87%",
            risk_control_1y="29%",
            volatility_rank_1y="0.342",
            sharpe_rank_1y="0.1",
            max_draw_down_1y="0.3616",
            investment_cost_performance_3y="89%",
            risk_control_3y="32%",
            volatility_rank_3y="0.3113",
            sharpe_rank_3y="1.15",
            max_draw_down_3y="0.3616",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.7131480579",
            nav_grl1y="4.3006448744",
            nav_grl2y="116.5266365063",
            nav_grl3y="173.9079654286",
            srank_l1m="5/1369",
            srank_l3m="4/1322",
            srank_l6m="6/1218",
            srank_l1y="7/1024",
            srank_l3y="24/609",
            srank_base="5/1381"
        )
        fund_two = fund.objects.create(
            id=10,
            fund_code="001070",
            fund_name="建信信息产业股票",
            fund_type="stock_fund",
            fund_asset_val="1019048105.05",
            stock_percent="80.83",
            cash_percent="18.11",
            bond_percent="0.98",
            other_percent="0.08",
            investment_cost_performance_1y="75%",
            risk_control_1y="55%",
            volatility_rank_1y="0.2572",
            sharpe_rank_1y="0.0",
            max_draw_down_1y="0.3358",
            investment_cost_performance_3y="78%",
            risk_control_3y="55%",
            volatility_rank_3y="0.2576",
            sharpe_rank_3y="0.99",
            max_draw_down_3y="0.3358",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.1773543422",
            nav_grl1y="13.4965266292",
            nav_grl2y="-23.4932709187",
            nav_grl3y="-0.3049942814",
            srank_l1m="1/1369",
            srank_l3m="1/1322",
            srank_l6m="7/1218",
            srank_l1y="4/1024",
            srank_l3y="20/609",
            srank_base="4/1381"
        )



        fund_to_fund_field_one = fundToFundField.objects.create(
            id=3,
            fund_id=fund_one,
            fund_field_id_id=1,
            fund_percent="16.48"
        )

        fund_to_fund_field_two = fundToFundField.objects.create(
            id=4,
            fund_id=fund_two,
            fund_field_id_id=1,
            fund_percent="62.44"
        )

        this.selected_risk_fund_list_for_one_or_two_years = [fund_to_fund_field_two]
        this.selected_bond_fund_list_for_one_or_two_years = [fund_one]
        result = self.client.get("/business/getSchemeOne?user_id=49")
        allFundData = json.loads(result.content)['data']
        print("方案一筛选出的股票如下")
        print(allFundData)


        this.selected_risk_fund_list_for_one_or_two_years = [fund_to_fund_field_one, fund_to_fund_field_two]
        this.selected_bond_fund_list_for_one_or_two_years = [fund_one]
        result = self.client.get("/business/getSchemeOne?user_id=49")
        allFundData = json.loads(result.content)['data']
        print("方案一筛选出的股票如下")
        print(allFundData)
        print("get_scheme_one方法测试完成")

    def test_get_scheme_two_controller(self):
        print("get_scheme_two方法测试开始")

        this = FundListSingleton.get_instance()
        fund_one = fund.objects.create(
            id=4,
            fund_code="003624",
            fund_name="创金合信资源主题精选股票A",
            fund_type="stock_fund",
            fund_asset_val="950924440.13",
            stock_percent="88.05",
            cash_percent="6.56",
            bond_percent="5.16",
            other_percent="0.23",
            investment_cost_performance_1y="87%",
            risk_control_1y="29%",
            volatility_rank_1y="0.342",
            sharpe_rank_1y="0.1",
            max_draw_down_1y="0.3616",
            investment_cost_performance_3y="89%",
            risk_control_3y="32%",
            volatility_rank_3y="0.3113",
            sharpe_rank_3y="1.15",
            max_draw_down_3y="0.3616",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.7131480579",
            nav_grl1y="4.3006448744",
            nav_grl2y="116.5266365063",
            nav_grl3y="173.9079654286",
            srank_l1m="5/1369",
            srank_l3m="4/1322",
            srank_l6m="6/1218",
            srank_l1y="7/1024",
            srank_l3y="24/609",
            srank_base="5/1381"
        )
        fund_two = fund.objects.create(
            id=11,
            fund_code="001070",
            fund_name="建信信息产业股票",
            fund_type="stock_fund",
            fund_asset_val="1019048105.05",
            stock_percent="80.83",
            cash_percent="18.11",
            bond_percent="0.98",
            other_percent="0.08",
            investment_cost_performance_1y="85%",
            risk_control_1y="55%",
            volatility_rank_1y="0.2572",
            sharpe_rank_1y="0.0",
            max_draw_down_1y="0.3358",
            investment_cost_performance_3y="78%",
            risk_control_3y="55%",
            volatility_rank_3y="0.2576",
            sharpe_rank_3y="0.99",
            max_draw_down_3y="0.3358",
            nav_grl1m="-5.8908507223",
            nav_grl3m="-10.7440532826",
            nav_grl6m="-4.1773543422",
            nav_grl1y="13.4965266292",
            nav_grl2y="-23.4932709187",
            nav_grl3y="-0.3049942814",
            srank_l1m="1/1369",
            srank_l3m="1/1322",
            srank_l6m="7/1218",
            srank_l1y="4/1024",
            srank_l3y="20/609",
            srank_base="4/1381"
        )

        fund_to_fund_field_one = fundToFundField.objects.create(
            id=5,
            fund_id_id=4,
            fund_field_id_id=1,
            fund_percent="16.48"
        )

        fund_to_fund_field_two = fundToFundField.objects.create(
            id=6,
            fund_id_id=11,
            fund_field_id_id=1,
            fund_percent="62.44"
        )
        this.selected_risk_fund_list_for_one_or_two_years = [fund_to_fund_field_one, fund_to_fund_field_two]
        this.selected_bond_fund_list_for_one_or_two_years = [fund_one,fund_two]
        result = self.client.get("/business/getSchemeTwo?user_id=49")
        allFundData = json.loads(result.content)['data']
        print("方案二筛选出的股票如下")
        print(allFundData)
        print("get_scheme_two方法测试完成")

    def test_fund_selection_rule_algori(self):
        print("fund_selection_rule方法测试开始")
        # fund_selection_rules没有返回值，所以直接在方法中打印出结果
        fund_selection_rule('1')

        fund.objects.create(
            id=100,
            fund_code="008866",
            fund_name="国寿安保尊耀纯债债券A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="10/3784",
            srank_l3m="9/3696",
            srank_l6m="45/3477",
            srank_l1y="100/3066",
            srank_base="77/3781"
        )

        fund.objects.create(
            id=101,
            fund_code="008867",
            fund_name="招商招坤纯债A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="77/3784",
            srank_l3m="80/3696",
            srank_l6m="44/3477",
            srank_l1y="1/3066",
            srank_base="220/3781"
        )

        fund.objects.create(
            id=102,
            fund_code="008868",
            fund_name="工银红利混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="32/3784",
            srank_l3m="10/3696",
            srank_l6m="4/3477",
            srank_l1y="9/3066",
            srank_base="45/3781"
        )

        fund.objects.create(
            id=103,
            fund_code="008869",
            fund_name="前海开源沪港深大消费C",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="14/3784",
            srank_l3m="8/3696",
            srank_l6m="4/3477",
            srank_l1y="8/3066",
            srank_base="22/3781"
        )

        fund.objects.create(
            id=104,
            fund_code="008870",
            fund_name="华宝国策导向混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="16/3784",
            srank_l3m="9/3696",
            srank_l6m="90/3477",
            srank_l1y="87/3066",
            srank_base="212/3781"
        )

        fund.objects.create(
            id=105,
            fund_code="008871",
            fund_name="华商大盘量化混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="124/3784",
            srank_l3m="48/3696",
            srank_l6m="42/3477",
            srank_l1y="7/3066",
            srank_base="202/3781"
        )

        fund.objects.create(
            id=106,
            fund_code="008872",
            fund_name="易方达安盈回报混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="147/3784",
            srank_l3m="84/3696",
            srank_l6m="46/3477",
            srank_l1y="59/3066",
            srank_base="92/3781"
        )

        fund.objects.create(
            id=107,
            fund_code="008873",
            fund_name="国泰景气行业混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="78/3784",
            srank_l3m="39/3696",
            srank_l6m="19/3477",
            srank_l1y="31/3066",
            srank_base="37/3781"
        )

        fund.objects.create(
            id=108,
            fund_code="008874",
            fund_name="鑫元欣享混合A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="43/3784",
            srank_l3m="74/3696",
            srank_l6m="99/3477",
            srank_l1y="81/3066",
            srank_base="20/3781"
        )

        fund.objects.create(
            id=109,
            fund_code="008875",
            fund_name="广发聚富财富",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="61/3784",
            srank_l3m="74/3696",
            srank_l6m="2/3477",
            srank_l1y="10/3066",
            srank_base="28/3781"
        )

        fundToFundField.objects.create(
            id=100,
            fund_id_id=100,
            fund_field_id_id=1,
            fund_percent="50.89"
        )
        fundToFundField.objects.create(
            id=101,
            fund_id_id=101,
            fund_field_id_id=1,
            fund_percent="5.89"
        )
        fundToFundField.objects.create(
            id=102,
            fund_id_id=102,
            fund_field_id_id=1,
            fund_percent="61.92"
        )
        fundToFundField.objects.create(
            id=103,
            fund_id_id=103,
            fund_field_id_id=1,
            fund_percent="26.74"
        )
        fundToFundField.objects.create(
            id=104,
            fund_id_id=104,
            fund_field_id_id=1,
            fund_percent="68.17"
        )
        fundToFundField.objects.create(
            id=105,
            fund_id_id=105,
            fund_field_id_id=1,
            fund_percent="37.55"
        )
        fundToFundField.objects.create(
            id=106,
            fund_id_id=106,
            fund_field_id_id=1,
            fund_percent="5.55"
        )
        fundToFundField.objects.create(
            id=107,
            fund_id_id=107,
            fund_field_id_id=1,
            fund_percent="3.33"
        )
        fundToFundField.objects.create(
            id=108,
            fund_id_id=108,
            fund_field_id_id=1,
            fund_percent="10.02"
        )
        fundToFundField.objects.create(
            id=109,
            fund_id_id=109,
            fund_field_id_id=1,
            fund_percent="4.41"
        )

        fund_selection_rule('1')
        fund_selection_rule('2')
        fund_selection_rule('3')
        fund_selection_rule('4')
        print("fund_selection_rule方法测试完成")

    # cal_weight_by_eigenvalue_method方法测试
    def test_AHP_cal_weight_by_eigenvalue_method_algori(self):
        print("AHP中的cal_weight_by_eigenvalue_method方法测试开始")
        # 定义一个2x2的矩阵
        programme_one = np.array(
            [[1, 1 / VERY_SIGNIFICANTLY_HIGHER_THAN_ANOTHER], [VERY_SIGNIFICANTLY_HIGHER_THAN_ANOTHER, 1]])
        print("计算得到的权重值为：")
        print(AHP(programme_one).cal_weight__by_eigenvalue_method())
        print("AHP中的cal_weight_by_eigenvalue_method方法测试完成")

    # fund_selection_rule_for_one_or_two方法测试
    def test_fund_selection_rule_for_one_or_two_algori(self):
        print("fund_selection_rule_for_one_or_two方法测试开始")
        print("fund_selection_rule_for_one_or_two方法测试完成")

    # bond_fund_selection_rules方法测试
    def test_bond_fund_selection_rules_algori(self):
        print("bond_fund_selection_rules方法测试开始")
        # bond_fund_selection_rules没有返回值，所以直接在方法中json格式打印出结果
        bond_fund_selection_rules('1')

        fund.objects.create(
            id=100,
            fund_code="008866",
            fund_name="国寿安保尊耀纯债债券A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="10/3784",
            srank_l3m="9/3696",
            srank_l6m="45/3477",
            srank_l1y="100/3066",
            srank_l3y="23/609",
            srank_base="77/3781"
        )

        fund.objects.create(
            id=101,
            fund_code="008867",
            fund_name="招商招坤纯债A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="77/3784",
            srank_l3m="80/3696",
            srank_l6m="44/3477",
            srank_l1y="1/3066",
            srank_l3y="27/609",
            srank_base="220/3781"
        )

        fund.objects.create(
            id=102,
            fund_code="008868",
            fund_name="工银红利混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="32/3784",
            srank_l3m="10/3696",
            srank_l6m="4/3477",
            srank_l1y="9/3066",
            srank_l3y="29/609",
            srank_base="45/3781"
        )

        fund.objects.create(
            id=103,
            fund_code="008869",
            fund_name="前海开源沪港深大消费C",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="14/3784",
            srank_l3m="8/3696",
            srank_l6m="4/3477",
            srank_l1y="8/3066",
            srank_l3y="31/609",
            srank_base="22/3781"
        )

        fund.objects.create(
            id=104,
            fund_code="008870",
            fund_name="华宝国策导向混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="16/3784",
            srank_l3m="9/3696",
            srank_l6m="90/3477",
            srank_l1y="87/3066",
            srank_l3y="42/609",
            srank_base="212/3781"
        )

        fund.objects.create(
            id=105,
            fund_code="008871",
            fund_name="华商大盘量化混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="124/3784",
            srank_l3m="48/3696",
            srank_l6m="42/3477",
            srank_l1y="7/3066",
            srank_l3y="50/609",
            srank_base="202/3781"
        )

        fund.objects.create(
            id=106,
            fund_code="008872",
            fund_name="易方达安盈回报混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="147/3784",
            srank_l3m="84/3696",
            srank_l6m="46/3477",
            srank_l1y="59/3066",
            srank_l3y="2/609",
            srank_base="92/3781"
        )

        fund.objects.create(
            id=107,
            fund_code="008873",
            fund_name="国泰景气行业混合",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="78/3784",
            srank_l3m="39/3696",
            srank_l6m="19/3477",
            srank_l1y="31/3066",
            srank_l3y="33/609",
            srank_base="37/3781"
        )

        fund.objects.create(
            id=108,
            fund_code="008874",
            fund_name="鑫元欣享混合A",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="43/3784",
            srank_l3m="74/3696",
            srank_l6m="99/3477",
            srank_l1y="81/3066",
            srank_l3y="67/609",
            srank_base="20/3781"
        )

        fund.objects.create(
            id=109,
            fund_code="008875",
            fund_name="广发聚富财富",
            fund_type="bond_fund",
            investment_cost_performance_1y="66%",
            risk_control_1y="33%",
            volatility_rank_1y="0.0043",
            sharpe_rank_1y="6.89",
            max_draw_down_1y="0.0011",
            nav_grl1m="0.4100270245",
            nav_grl3m="0.8328654314",
            nav_grl6m="1.7277190332",
            nav_grl1y="4.469652899",
            nav_grl2y="7.8146888133",
            srank_l1m="61/3784",
            srank_l3m="74/3696",
            srank_l6m="2/3477",
            srank_l1y="10/3066",
            srank_l3y="19/609",
            srank_base="28/3781"
        )

        fundToFundField.objects.create(
            id=100,
            fund_id_id=100,
            fund_field_id_id=1,
            fund_percent="50.89"
        )
        fundToFundField.objects.create(
            id=101,
            fund_id_id=101,
            fund_field_id_id=1,
            fund_percent="5.89"
        )
        fundToFundField.objects.create(
            id=102,
            fund_id_id=102,
            fund_field_id_id=1,
            fund_percent="61.92"
        )
        fundToFundField.objects.create(
            id=103,
            fund_id_id=103,
            fund_field_id_id=1,
            fund_percent="26.74"
        )
        fundToFundField.objects.create(
            id=104,
            fund_id_id=104,
            fund_field_id_id=1,
            fund_percent="68.17"
        )
        fundToFundField.objects.create(
            id=105,
            fund_id_id=105,
            fund_field_id_id=1,
            fund_percent="37.55"
        )
        fundToFundField.objects.create(
            id=106,
            fund_id_id=106,
            fund_field_id_id=1,
            fund_percent="5.55"
        )
        fundToFundField.objects.create(
            id=107,
            fund_id_id=107,
            fund_field_id_id=1,
            fund_percent="3.33"
        )
        fundToFundField.objects.create(
            id=108,
            fund_id_id=108,
            fund_field_id_id=1,
            fund_percent="10.02"
        )
        fundToFundField.objects.create(
            id=109,
            fund_id_id=109,
            fund_field_id_id=1,
            fund_percent="4.41"
        )
        bond_fund_selection_rules('1')
        bond_fund_selection_rules('2')
        bond_fund_selection_rules('3')
        bond_fund_selection_rules('4')
        print("bond_fund_selection_rules方法测试完成")

    # get_weight_by_ahp方法测试
    def test_get_weight_by_ahp_algori(self):
        print("get_weight_by_ahp方法测试开始")
        result = get_weight_by_ahp(100)
        print('投机心理指标为:')
        print(result[0])
        print('抗风险能力指标为：')
        print(result[1])
        print('短期指标为：')
        print(result[2])
        self.assertEqual(result[2], 0.125)
        print('长期指标为：')
        print(result[3])
        self.assertEqual(result[3], 0.875)
        print("get_weight_by_ahp方法测试完成")

    # get_user_fund_plan方法测试
    def test_get_user_fund_plan_controller(self):
        print("get_user_fund_plan方法测试开始")

        data = {'user_id': ['1'], 'fund_plan_list': ['[{"fund_id":1,"fund_code":"003545","fund_name":"东兴兴利债券A",'
                                                     '"fund_proportion":0.10841248303934871,"fund_type":"bond_fund",'
                                                     '"volatility_rank_1y":"0.0045","sharpe_rank_1y":"9.54",'
                                                     '"max_draw_down_1y":"0.0011","volatility_rank_3y":"0.0059",'
                                                     '"sharpe_rank_3y":"5.81","max_draw_down_3y":"0.003","fund_show_type":"债券型基金"}]']}
        result = self.client.post("/business/getUserFundPlan", data=data)
        self.assertEqual(result.status_code, 200)
        self.assertEqual(json.loads(result.content)['data'], "success")
        self.assertTrue(json.loads(result.content)['flag'])
        print("get_user_fund_plan方法测试完成")

    # pay_verify方法测试
    def test_pay_verify_controller(self):
        print("pay_verify方法测试开始")
        # 余额支付成功
        data = {'operate_type': ['buy'], 'transaction_amount': ['100'], 'transaction_source': ['1'], 'user_id': ['1'], 'buy_password': ['123456789']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "成功支付!")
        self.assertTrue(json.loads(result.content)['flag'])
        # 余额支付失败（余额不足）
        data = {'operate_type': ['buy'], 'transaction_amount': ['100000'], 'transaction_source': ['1'], 'user_id': ['1'],
                'buy_password': ['123456789']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "此用户账户的余额不足!")
        self.assertFalse(json.loads(result.content)['flag'])
        # 余额支付失败（密码错误）
        data = {'operate_type': ['buy'], 'transaction_amount': ['100'], 'transaction_source': ['1'],
                'user_id': ['1'],
                'buy_password': ['12345678910']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "支付密码错误!")
        self.assertFalse(json.loads(result.content)['flag'])

        # 银行卡支付成功
        data = {'operate_type': ['buy'], 'transaction_amount': ['100'], 'transaction_source': ['1111111111111111'], 'user_id': ['1'], 'buy_password': ['123456']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "成功支付!")
        self.assertTrue(json.loads(result.content)['flag'])
        # 银行卡支付失败（银行卡余额不足）
        data = {'operate_type': ['buy'], 'transaction_amount': ['10000'], 'transaction_source': ['1111111111111111'], 'user_id': ['1'], 'buy_password': ['123456']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "此银行卡的余额不足!")
        self.assertFalse(json.loads(result.content)['flag'])
        # 银行卡支付失败（密码错误）
        data = {'operate_type': ['buy'], 'transaction_amount': ['10000'], 'transaction_source': ['1111111111111111'], 'user_id': ['1'], 'buy_password': ['123456789']}
        result = self.client.post("/user/payVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "支付密码错误!")
        self.assertFalse(json.loads(result.content)['flag'])
        print("pay_verify方法测试完成")

    # sell_verify方法测试
    def test_sell_verify_controller(self):
        print("sell_verify方法测试开始")
        # 转出至余额成功
        data = {'operate_type': ['sell'], 'transaction_amount': ['25'], 'transaction_source': ['1'], 'user_id': ['1'], 'sell_password': ['123456789']}
        result = self.client.post("/user/sellVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "成功转出!")
        self.assertTrue(json.loads(result.content)['flag'])
        # 转出至余额失败（密码错误）
        data = {'operate_type': ['sell'], 'transaction_amount': ['25'], 'transaction_source': ['1'], 'user_id': ['1'],
                'sell_password': ['12345678910']}
        result = self.client.post("/user/sellVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "转出密码错误!")
        self.assertFalse(json.loads(result.content)['flag'])

        # 转出至银行卡成功
        data = {'operate_type': ['sell'], 'transaction_amount': ['25'], 'transaction_source': ['1111111111111111'], 'user_id': ['1'], 'sell_password': ['123456']}
        result = self.client.post("/user/sellVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "成功转出!")
        self.assertTrue(json.loads(result.content)['flag'])
        # 转出至银行卡失败（密码错误）
        data = {'operate_type': ['sell'], 'transaction_amount': ['25'], 'transaction_source': ['1111111111111111'], 'user_id': ['1'], 'sell_password': ['12345678']}
        result = self.client.post("/user/sellVerify", data=data)
        self.assertEqual(json.loads(result.content)['data'], "转出密码错误!")
        self.assertFalse(json.loads(result.content)['flag'])
        print("sell_verify方法测试完成")

    def tearDown(self) -> None:
        print("--------------------------")




